package com.example.managesystem.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.example.managesystem.constant.MessageConstant;
import com.example.managesystem.context.BaseContext;
import com.example.managesystem.dto.AnnoDTO;
import com.example.managesystem.dto.AnnoPageQueryDTO;
import com.example.managesystem.entity.Anno;
import com.example.managesystem.entity.User;
import com.example.managesystem.exception.AccountNotFoundException;
import com.example.managesystem.exception.BaseException;
import com.example.managesystem.mapper.UserItemRelaMapper;
import com.example.managesystem.mapper.UserMapper;
import com.example.managesystem.vo.AnnoMaintextVO;
import com.example.managesystem.vo.AnnoVO;
import com.example.managesystem.vo.AnnosVO;
import com.example.managesystem.mapper.AnnoMapper;
import com.example.managesystem.service.AnnoService;
import com.example.managesystem.vo.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class AnnoServiceImpl implements AnnoService {

    @Autowired
    private AnnoMapper annoMapper;
    @Autowired
    private UserItemRelaMapper userItemRelaMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取所有公告
     * @return
     */
    @Override
    public AnnosVO getAnnos() {
        List<AnnoVO>  annoList= annoMapper.queryAllAnno();
        AnnosVO annosVO=new AnnosVO();
        annosVO.setAnnoList(annoList);
        return annosVO;
    }

    /**
     * 获取公告详情
     *
     * @param id
     * @return
     */
    @Override
    public AnnoMaintextVO getAnno(Integer id) {
        AnnoMaintextVO anno = annoMapper.getAnnoById(id);
        return anno;
    }

    /**
     * 分页查询公告
     * @param annoPageQueryDTO
     * @return
     */
    @Override
    public PageResult pagequeryAnno(AnnoPageQueryDTO annoPageQueryDTO) {
        // PageHelper通过这个插件，可以查询到页码和页记录数，然后可以和后面的SQL语句拼接到一块
        PageHelper.startPage(annoPageQueryDTO.getPage(),annoPageQueryDTO.getPageSize());
        // 按所给条件查询SQL
        Page<AnnoVO> page=annoMapper.pagequeryByDTO(annoPageQueryDTO);

        Long total=page.getTotal();
        List<AnnoVO> records=page.getResult();
        PageResult pageResult = new PageResult(total, records);
        System.out.println(pageResult);

        return pageResult;
    }

    /**
     * 批量删除公告
     * @param annoids
     * @return
     */
    @Override
    public void deleteAnnos(List<Long> annoids) {
        log.info("批量删除公告");
        //先验证当前用户是否为老师 有没有权限删除公告
        //获取当前登录的userid(在设置拦截器时，将usrid设置到 BaseContext的ThreadLocal中)
        Long id= BaseContext.getCurrentId();
        String userid=String.valueOf(id);
        log.info("当前用户号为{}",userid);

        Integer count=userItemRelaMapper.queryTeacherExistByUserIdWithRole(userid,3);
        log.info("当前用户为{}个项目的审核老师",count);
        if(count!=0){
            annoMapper.deleteAnnos(annoids);
        }else{
            throw new BaseException(MessageConstant.USER_NOAUTHORITY);
            /*log.info("当前用户无权删除公告");*/
        }




    }

    /**
     * 发布公告
     * @param annoDTO
     * @return
     */
    @Override
    public void postAnno(AnnoDTO annoDTO) {
        log.info("发布公告");
        //先验证当前用户是否为负责人 有没有权限删除公告
        //获取当前登录的userid(在设置拦截器时，将usrid设置到 BaseContext的ThreadLocal中)
        Long id= BaseContext.getCurrentId();
        String userid=String.valueOf(id);
        log.info("当前用户号为{}",userid);
        User user=userMapper.getByUserId(userid);
        // 如果是公告不为空，修改公告
        // 如果公告id为空，创建公告
        if(annoDTO.getId()!=null){
            annoMapper.updateAnno(annoDTO);
        }else{
            Anno anno=new Anno();
            BeanUtils.copyProperties(annoDTO,anno);
            anno.setCreate_time(LocalDate.now());

            log.info("公告发布者部门为："+user.getDept());
            anno.setDept(user.getDept());

            Integer count1=userItemRelaMapper.queryTeacherExistByUserIdWithRole(userid,0);
            Integer count2=userItemRelaMapper.queryTeacherExistByUserIdWithRole(userid,0);
            Integer count=count1+count2;
            log.info("当前用户为{}个项目的负责人",count);
            if(count!=0){
                annoMapper.insertAnno(anno);
            }else{
                throw new BaseException(MessageConstant.USER_NOAUTHORITY);
                /*log.info("当前用户无权新增公告");*/
            }
        }


    }
}
